package com.auto.learning.adapter;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.AnchorInfoActivity;
import com.auto.learning.bean.AnchorBean;
import com.auto.learning.view.CircleImageView;
import com.auto.learning.view.MyTextView;
import com.nostra13.universalimageloader.core.ImageLoader;


public class AnchorAdapter extends BaseAdapter {
	
	
	private ArrayList<AnchorBean> list ;
	private Context context;
	private int type = 0;
	
	public AnchorAdapter(Context context,ArrayList<AnchorBean> list){
		this.context = context;
		this.list = list;
	}
	
	public AnchorAdapter(Context context,ArrayList<AnchorBean> list,int type){
		this.context = context;
		this.list = list;
		this.type = type;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	ViewHolder holder;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final AnchorBean anchorBean = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.anchor_adapter, null);
			holder = new ViewHolder();
			holder.image_a_face = (CircleImageView) convertView.findViewById(R.id.image_a_face);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.style = (TextView) convertView.findViewById(R.id.style);
			holder.arce = (TextView) convertView.findViewById(R.id.arce);
			holder.intro = (TextView) convertView.findViewById(R.id.intro);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rat_num);
			holder.tv_anchor_favor_number = (TextView) convertView.findViewById(R.id.tv_anchor_favor_number);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		ImageLoader.getInstance().displayImage(anchorBean.getFace(),holder.image_a_face);
		holder.name.setText(anchorBean.getName());
		holder.style.setText(anchorBean.getStyle());
		holder.arce.setText(anchorBean.getArea());
		holder.intro.setText(anchorBean.getIntro());
		holder.tv_anchor_favor_number.setText(anchorBean.getFavorNum()+"");
		holder.ratingBar.setNumStars(anchorBean.getStars());
		if (type == 1) {
			holder.intro.setVisibility(View.VISIBLE);
		}else if (type == 0){
			holder.intro.setVisibility(View.GONE);
		}
		holder.image_a_face.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent mIntent = new Intent(context, AnchorInfoActivity.class);
				mIntent.putExtra("m_anchor_bean", anchorBean);
				context.startActivity(mIntent);				
			}
		});
		convertView.setBackgroundColor(context.getResources().getColor(R.color.main_backgroud_item));
		return convertView;
	}
	
	class ViewHolder{
		CircleImageView image_a_face;
		TextView name,style,arce,tv_anchor_favor_number;
		TextView intro;
		LinearLayout linear_favor;
		RatingBar ratingBar;
	}

}
